package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 套餐管理
 *
 * @author admin
 * @date 2024/9/15
 */
@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     */
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(StatusConstant.DISABLE);
        BeanUtils.copyProperties(setmealDTO, setmeal);

        setmealMapper.insert(setmeal);
        //     插入套餐表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        // 获取套餐id
        Long id = setmeal.getId();

        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(s -> s.setSetmealId(id));
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 分页查询
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //     判断套餐是否在售
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.getById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //     删除套餐
        setmealMapper.delete(ids);
        setmealDishMapper.deleteBatch(ids);
    }

    /**
     * 根据id查询套餐和菜品
     */
    @Override
    public SetmealVO getByIdWithDishes(Long id) {
        // 查询套餐信息
        Setmeal setmeal = setmealMapper.getById(id);

        // 查询菜品信息
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal, setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 修改套餐
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //     修改套餐
        setmealMapper.update(setmeal);

        //     修改套餐下菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        Long id = setmealDTO.getId();
        //     将id转换为list集合
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        //     先删除，再添加
        setmealDishMapper.deleteBatch(ids);

        if (setmealDishes != null && setmealDishes.size() > 0) {
            setmealDishes.forEach(s -> s.setSetmealId(id));
        }
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 启用停用套餐
     */
    @Override
    public void startOrStop(Integer status, Long id) {
    //     查询套餐内是否有停售菜品
        if(StatusConstant.ENABLE == status) {
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if(dishList != null && dishList.size() > 0) {
                for (Dish dish : dishList) {
                    if(StatusConstant.DISABLE == dish.getStatus()) {
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                }
            }
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }

    /**
     * 根据分类id查询套餐
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id返回菜品
     */
    @Override
    public List<DishItemVO> getDishesById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
